package com.xy.mainp.huaban.activity;

import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.xy.img.GlideUtil;
import com.xy.mainp.R;
import com.xy.mainp.base.BasePActivity;
import com.xy.mainp.base.TittleBarActivity;
import com.xy.mainp.databinding.ActivityHuabanImgDetailBinding;
import com.xy.mainp.huaban.widget.ImgVpTittle;
import com.xy.mainp.huaban.widget.VpImgDetailView;
import com.xy.mainp.main.activity.MainActivity;
import com.xy.mainp.model.huaban.ImgEntity;
import com.xy.view.SmoothImageView;
import com.xy.view.TouchImageView;

import java.util.ArrayList;

/**
 * Created by John on 2016/11/22.
 */

public class ImgDetailActivity extends TittleBarActivity  {

    private ActivityHuabanImgDetailBinding binding;
    private int mPosition;
    private ArrayList<ImgEntity> mDatas;
    private ArrayList<VpImgDetailView> imgs;
    private ArrayList<ImgVpTittle> imgTittles;

    @Override
    protected void initData() {
        super.initData();
        mDatas = (ArrayList<ImgEntity>) getIntent().getSerializableExtra("images");
        mPosition = getIntent().getIntExtra("position", 0);
        imgs = new ArrayList<>();
        imgTittles = new ArrayList<>();
        for (int i = 0; i < mDatas.size(); i++) {
            VpImgDetailView view = new VpImgDetailView(this);
            view.setData(mDatas.get(i));
            imgs.add(view);

            ImgVpTittle imgVpTittle = new ImgVpTittle(this);
            imgVpTittle.setData(mDatas.get(i));
            imgTittles.add(imgVpTittle);
        }
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.activity_huaban_img_detail, null, true);
        setContentView(binding.getRoot());
        tittleBar.canBack(true);
        initViewPager();
    }


    public static void enter(Intent intent, Activity activity) {
        intent.setClass(activity, ImgDetailActivity.class);
        activity.startActivityForResult(intent,1);
    }

    private void initViewPager() {
        binding.vpHuabanImgDetail.setAdapter(new ImgDetailAdapter(imgs));
        binding.vpHuabanImgDetail.setCurrentItem(mPosition);
        tittleBar.setTittle(imgTittles.get(mPosition));
        binding.vpHuabanImgDetail.addOnPageChangeListener(pageChangeListener);
    }

    private ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            tittleBar.setTittle(imgTittles.get(position));
            Intent intent = new Intent();
            intent.putExtra("selectedPosition",position);
            ImgDetailActivity.this.setResult(RESULT_OK,intent);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    class ImgDetailAdapter extends PagerAdapter {

        private ArrayList<VpImgDetailView> datas;

        public ImgDetailAdapter(ArrayList<VpImgDetailView> datas) {
            this.datas = datas;
        }

        @Override
        public int getCount() {
            return datas == null ? 0 : datas.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((VpImgDetailView) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            VpImgDetailView view = datas.get(position);
            container.addView(view);
            return datas.get(position);
        }
    }
}
